package com.ruicar.afs.cloud.seats.controller;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.seats.entity.PostChangeLog;
import com.ruicar.afs.cloud.seats.service.PostChangeLogService;
import com.ruicar.afs.cloud.seats.util.PostStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author tiankai
 * @Describe 岗位异动日志接口
 * Created on 2020-06-18
 */
@Slf4j
@RestController
@RequestMapping("/group")
@AllArgsConstructor
@Api(description = "岗位异动日志接口")
public class PostChangeLogController {

    private PostChangeLogService service;

    @RequestMapping(value = "/savePostChangeLog", method = RequestMethod.POST)
    @ApiOperation(value = "岗位异动日志")
    public IResponse<PostChangeLog> savePostChangeLog(@RequestBody PostChangeLog log) {
        PostChangeLog postChangeLog = new PostChangeLog();
        postChangeLog.setUserId(log.getUserId());
        postChangeLog.setUserName(log.getUserName());
        postChangeLog.setPostChangeStatus(log.getPostChangeStatus());
        postChangeLog.setLoginName(log.getLoginName());
        if(PostStatus.YES.getCode().equals(log.getPostChangeStatus())){
            postChangeLog.setChangeRemarks("手动上岗");
        }else{
            postChangeLog.setChangeRemarks(log.getChangeRemarks());
        }
        service.saveOrUpdate(postChangeLog);
        return new IResponse().setData(true);
    }

    @GetMapping(value = {"/queryPostChangeLog"})
    @ApiOperation(value = "岗位异动日志查询")
    public IResponse<PostChangeLog> queryPostChangeLog(@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize, @RequestParam(required = false) String userId) {
        if(StringUtil.isEmpty(userId)){
            userId = SecurityUtils.getUsername();
        }
        IPage<PostChangeLog> list = service.page(new Page(pageNumber, pageSize), Wrappers.<PostChangeLog>query().lambda().eq(PostChangeLog::getLoginName, userId).orderByDesc(PostChangeLog::getCreateTime));
        return IResponse.success(list);
    }
}
